package com.ruoyi.monitorTools.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.monitorTools.domain.HotSpotRegion;
import com.ruoyi.monitorTools.domain.RegionalInfo;
import com.ruoyi.monitorTools.mapper.HotSpotRegionMapper;
import com.ruoyi.monitorTools.service.HotSpotRegionService;
import com.ruoyi.monitorTools.service.RegionalInfoService;
import com.ruoyi.monitorTools.task.RegularlyModifyEnableStatusTasks;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
* @author Administrator
* @description 针对表【a2_hot_spot_region(热点区域表)】的数据库操作Service实现
* @createDate 2025-02-17 13:46:08
*/
@Service
public class HotSpotRegionServiceImpl extends ServiceImpl<HotSpotRegionMapper, HotSpotRegion>
    implements HotSpotRegionService {

    @Autowired
    private RegionalInfoService regionalInfoService;

    @Autowired
    private HotSpotRegionMapper hotSpotRegionMapper;

    @Override
    public int insertOne(HotSpotRegion hotSpotRegion) {
        if(StringUtils.isNull(hotSpotRegion.getRegionId())) {
            //设置区域范围
            RegionalInfo regionalInfo = new RegionalInfo();
            regionalInfo.setRegionGeojson(hotSpotRegion.getRegionGeojson());
            regionalInfo.setRegionName(hotSpotRegion.getRegionName());
            Long regionalId = regionalInfoService.addRegional(regionalInfo);
            hotSpotRegion.setRegionId(regionalId);
        }
        hotSpotRegion.setCreateTime(new Date());
        return hotSpotRegionMapper.insert(hotSpotRegion);

    }

    @Override
    public HotSpotRegion selectHotById(Long id) {
        HotSpotRegion hotSpotRegion = hotSpotRegionMapper.selectById(id);
        RegionalInfo regionalInfo = regionalInfoService.getById(hotSpotRegion.getRegionId());
        hotSpotRegion.setRegionGeojson(regionalInfo.getRegionGeojson());
        hotSpotRegion.setRegionName(regionalInfo.getRegionName());
        return hotSpotRegion;
    }

    @Override
    public List<HotSpotRegion> listByPage(String regionName, Integer enableStatus) {
        return hotSpotRegionMapper.listByPage(regionName, enableStatus);
    }

    @Override
    public List<RegionalInfo> selectOtherList() {
        QueryWrapper<HotSpotRegion> eleQueryWrap = new QueryWrapper<>();
        eleQueryWrap.select("region_id");
        List<HotSpotRegion> hotSpotRegions = hotSpotRegionMapper.selectList(eleQueryWrap);
        List<Long> collect = hotSpotRegions.stream().filter(ele -> ele != null).map(ele -> ele.getRegionId()).collect(Collectors.toList());
        QueryWrapper<RegionalInfo> regionWrapper = new QueryWrapper<>();
        regionWrapper.select("id", "region_name")
                .notIn(CollectionUtil.isNotEmpty(collect), "id", collect);
        List<RegionalInfo> list = regionalInfoService.list(regionWrapper);
        return list;
    }

    @Override
    public AjaxResult batchUpdateEnableStatus(ArrayList<Integer> idList, Integer enableStatus) {
        try {
            UpdateWrapper<HotSpotRegion> wrapper = new UpdateWrapper<>();
            wrapper.in("task_id", idList);
            wrapper.set("enable_status", enableStatus);
            hotSpotRegionMapper.update(null, wrapper);
            return AjaxResult.success();
        } catch (Exception e) {
            log.error("修改失败",  e);
            return AjaxResult.error();
        }
    }

    @Override
    public List<HotSpotRegion> isBreakOrOut(double lon, double lat, Date baseTime) {
        return hotSpotRegionMapper.isContain(lon, lat, baseTime);
    }

    @Override
    public List<Map<String, Object>> getTargetSizeByRegion() {
        List<Map<String, Object>> returnMapList = new ArrayList<>();
//        RegularlyModifyEnableStatusTasks.targetNum.forEach((k, v) -> {
//            Map<String, Object> returnMap = new HashMap<>();
//            returnMap.put("size", v.size());
//            returnMap.put("geoJson", getRegionGeoJsonByTaskId(k));
//            returnMapList.add(returnMap);
//        });
//        return returnMapList;

        QueryWrapper<HotSpotRegion> queryWrap = new QueryWrapper<>();
        queryWrap.eq("enable_status", 1);
        List<HotSpotRegion> hotSpotRegions = hotSpotRegionMapper.selectList(queryWrap);
        hotSpotRegions.forEach(hotSpotRegion -> {
            Map<String, Object> returnMap = new HashMap<>();
            returnMap.put("taskId", hotSpotRegion.getTaskId());
            //todo 与电子围栏融合
//            returnMap.put("size", RegularlyModifyEnableStatusTasks.targetNum.get(hotSpotRegion.getTaskId()) != null ? RegularlyModifyEnableStatusTasks.targetNum.get(hotSpotRegion.getTaskId()).size() : 0);
            returnMap.put("geoJson", getRegionGeoJsonByTaskId(hotSpotRegion.getTaskId()));
            returnMapList.add(returnMap);
        });
        return returnMapList;

    }

    public String getRegionGeoJsonByTaskId(Long taskId){
        HotSpotRegion hotSpotRegion = hotSpotRegionMapper.selectById(taskId);
        RegionalInfo regionalInfo = regionalInfoService.getById(hotSpotRegion.getRegionId());
        return regionalInfo.getRegionGeojson();
    }
}




